package com.banfan.almond.web.service.impl;

import com.banfan.almond.base.enums.EStatus;
import com.banfan.almond.base.global.MessageConf;
import com.banfan.almond.base.global.SQLConf;
import com.banfan.almond.base.serviceImpl.SuperServiceImpl;
import com.banfan.almond.entity.entity.Blog;
import com.banfan.almond.entity.vo.BlogVO;
import com.banfan.almond.utils.ResultUtil;
import com.banfan.almond.web.mapper.BlogMapper;
import com.banfan.almond.web.service.AdminService;
import com.banfan.almond.web.service.BlogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class BlogServiceImpl extends SuperServiceImpl<BlogMapper, Blog> implements BlogService {


    @Autowired
    private BlogService blogService;

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private AdminService adminService;

    @Override
    public String getUserBlog(Integer userUid,Integer currentPage) {

        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConf.USER_UID, userUid);
        queryWrapper.select(SQLConf.TITLE, SQLConf.UID, SQLConf.CREATE_TIME);
        Page<Blog> blogPage = new Page<>(currentPage, 10);
        List<Blog> records = blogMapper.selectPage(blogPage, queryWrapper).getRecords();
        return ResultUtil.successWithData(records);

    }

    @Override
    public String getUserBlogCount(Integer userUid) {
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConf.USER_UID, userUid);
        queryWrapper.select(SQLConf.UID);
        Integer integer = blogMapper.selectCount(queryWrapper);
        return ResultUtil.successWithData(integer);

    }

    @Override
    public String getHotBlog() {
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SQLConf.TITLE, SQLConf.UID, SQLConf.CREATE_TIME);
        Page<Blog> blogPage = new Page<>(1, 10);
        List<Blog> records = blogMapper.selectPage(blogPage, queryWrapper).getRecords();
        return ResultUtil.successWithData(records);
    }


    @Override
    public String addBlog(BlogVO blogVO) {

        Integer blogUid = blogVO.getBlogUid();
        Integer userUid = blogVO.getUserUid();


        Blog blog = new Blog();
        blog.setUid(blogVO.getUid());
        blog.setUserUid(blogVO.getUserUid());
        blog.setAuthor(blogVO.getAuthor());
        blog.setArticlesPart(blogVO.getArticlesPart());
        blog.setTitle(blogVO.getTitle());
        blog.setSummary(blogVO.getSummary());
        blog.setContent(blogVO.getContent());
        blog.setTagUid(blogVO.getTagUid());
        blog.setBlogSortUid(blogVO.getBlogSortUid());
        blog.setFileUid(blogVO.getFileUid());
        blog.setIsOriginal(blogVO.getIsOriginal());
        blog.setIsPublish(blogVO.getIsPublish());
        blog.setOutsideLink(blogVO.getOutsideLink());
        blog.setStatus(EStatus.ENABLE);

        //添加
        if (blogUid == null) {
            Boolean isSave = blogService.save(blog);
            if (isSave) {
                log.info("insert:userUid={},blogUid={}", userUid, blog.getUid());
                return ResultUtil.successWithData(blog.getUid());
            } else {
                return ResultUtil.errorWithMessage("异常blogUid=" + blogUid.toString());
            }
        }
        //更新
        else {
            QueryWrapper<Blog> wrapper = new QueryWrapper<>();
            wrapper.eq(SQLConf.UID, blogUid);
            wrapper.eq(SQLConf.USER_UID, userUid);
            Blog one = blogService.getOne(wrapper);
            if (one == null) {
                log.info("not exist blog update:userUid={},blogUid={}", userUid, blogUid);
            } else {
                blogService.update(blog,wrapper);
                log.info("update:userUid={},blogUid={}", userUid, blogUid);
            }
        }

//        updateSolrAndRedis(isSave, blog);
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String deleteBlog(Integer userUid,Integer blogUid) {

        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.eq(SQLConf.UID, blogUid);
        wrapper.eq(SQLConf.USER_UID, userUid);
        Blog blog1 = blogMapper.selectOne(wrapper);
        if (blog1 == null) {
            return ResultUtil.errorWithMessage("error delete");
        } else {
            int isDelete = blogMapper.deleteById(blog1.getUid());
            return ResultUtil.successWithData(isDelete);
        }

    }
}
